package org.blogwu.learn.designpattern.chapter8.hungry;

import java.io.Serializable;

/**
 * 饿汉式
 */
public class HungrySingleton implements Serializable {

    private final static HungrySingleton hungrySingleton = new HungrySingleton();

    private HungrySingleton() {
        /**
         * 此方法主要作用是防止反射 生成此类实例，破坏单例。
         */
        if (hungrySingleton != null) {
            throw new RuntimeException("禁止反射");
        }
    }

    public static HungrySingleton getInstance() {
        return hungrySingleton;
    }

    /**
     * 此方法主要作用是防止反序列化 生成此类实例，破坏单例。
     *
     * @return
     */
    public Object readResolve() {
        return HungrySingleton.getInstance();
    }

}
